﻿using System;
using System.Threading;

namespace clockEvent
{
    //处理tick的委托
    public delegate void TickHandler(object sender);
    //处理Alarm的委托
    public delegate void AlarmHandler(object sender);

   
    public class AlarmClock
    {
        private int alarmHour, alarmMinute, alarmSecond;
        public event TickHandler OnTick;
        public event AlarmHandler OnAlarm;
        
        public void SetAlarm(int h,int m,int s)
        {
            //设置闹钟响铃时间
            alarmHour = h;
            alarmMinute = m;
            alarmSecond = s;
        }

        public void TurnOn()
        {
            int hour, minute, second;
            //启动闹钟
            while(true){
                Thread.Sleep(1000);
                hour = DateTime.Now.Hour;
                minute = DateTime.Now.Minute;
                second = DateTime.Now.Second;
                ClockTick(hour,minute,second);
                if ( hour == alarmHour && minute == alarmMinute && second == alarmSecond)
                    ClockAlarm();
            }
        }

        public void ClockTick(int h,int m,int s)
        {
            //Console.WriteLine("The alarm clock ticks.");
            Console.Write(h + ":" + m + ":" + s + " ");
            OnTick(this);

        }

        public void ClockAlarm()
        {
            //Console.WriteLine("The alarm clock alarms.");
           
            OnAlarm(this);
        }
    }

    public class Manage
    {
        public AlarmClock clock1 = new AlarmClock();

        public Manage()
        {
            //设置闹钟响铃间隔
           
            clock1.OnTick += new TickHandler(Clock_Tick);
            clock1.OnAlarm += new AlarmHandler(Clock_Alarm);
        }

        void Clock_Tick(object sender)
        {
            Console.WriteLine("Tick!");
        }

        void Clock_Alarm(object sender)
        {
            Console.WriteLine("Alarm!");
        }


    }

    class Program
    {
        static void Main(string[] args)
        {
            Manage m1 = new Manage();
            m1.clock1.SetAlarm(16,46,0);//设置响铃时间
            m1.clock1.TurnOn();//启动闹钟
        }
    }
   
}
